package service

import (
	"context"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _enterpriseService struct {
}

func (s _enterpriseService) GetByName(ctx context.Context, name string) (*model.EnterpriseEntity, error) {
	return dao.EnterpriseEntity.WithContext(ctx).Where(dao.EnterpriseEntity.Name.Eq(name)).First()
}

func (s _enterpriseService) CreateApi(ctx context.Context, tx *dao.QueryTx, model *model.EnterpriseEntity) error {
	if err := tx.EnterpriseEntity.WithContext(ctx).Create(model); err != nil {
		return err
	}
	return nil
}

func (s _enterpriseService) GetByIdApi(ctx context.Context, id int64) (*model.EnterpriseEntity, error) {
	return dao.EnterpriseEntity.WithContext(ctx).Where(dao.EnterpriseEntity.ID.Eq(id)).First()
}

func (s _enterpriseService) ModifyStatusByIdApi(ctx context.Context, tx *dao.QueryTx, id, status int64) error {
	if _, err := tx.EnterpriseEntity.WithContext(ctx).Where(dao.EnterpriseEntity.ID.Eq(id)).UpdateSimple(
		dao.EnterpriseEntity.Status.Value(status),
	); err != nil {
		return err
	}
	return nil
}

func (s _enterpriseService) DeleteByIdApi(ctx context.Context, tx *dao.QueryTx, id int64) error {
	if _, err := tx.EnterpriseEntity.WithContext(ctx).Where(dao.EnterpriseEntity.ID.Eq(id)).Delete(); err != nil {
		return err
	}
	return nil
}

var EnterpriseService = new(_enterpriseService)
